package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.OfficeVO;
import edu.zqu.instructionalMatters.project.entity.Office;
import edu.zqu.instructionalMatters.project.mapper.OfficeMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import edu.zqu.instructionalMatters.project.service.FieldService;
import edu.zqu.instructionalMatters.project.service.FieldTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 办公室 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/office")
@Api(tags = "办公室")
public class OfficeController {
    @Autowired
    private OfficeMapper officeMapper;
    @Autowired
    private FieldService fieldService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private FieldTypeService fieldTypeService;

    @ApiOperation(value = "获取办公室列表")
    @RequestMapping(value = "/offices", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "fieldTypeId", value = "场地类型id", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId,
                             Integer fieldTypeId
    ) {
        Office office1 = new Office().setCollegeId(collegeId).setFieldTypeId(fieldTypeId);          //合成对象
        List<Office> list1 = officeMapper.findAll(office1);                                         //查出List<Office>
        List<OfficeVO> list = new ArrayList<>();                                                    //初始化List<OfficeVO>
        for (Office office : list1) {                                                                //循环遍历
            OfficeVO officeVO = new OfficeVO();                                                     //初始化VO
            BeanUtils.copyProperties(office, officeVO);                                              //对象复制赋值
            String collegeName = collegeService.getCollegeName(office.getCollegeId());              //获取学校名称
            String fieldName = fieldService.getFieldName(office.getFieldId());                      //获取场地名称
            String fieldTypeName = fieldTypeService.getFieldTypeName(office.getFieldTypeId());      //获取场地类型名称
            officeVO.setCollegeName(collegeName);                                                   //设置学校名称
            officeVO.setFieldName(fieldName);                                                       //设置场地名称
            officeVO.setFieldTypeName(fieldTypeName);                                               //设置场地类型名称
            list.add(officeVO);                                                                     //加入到list
        }
        return ZQUResult.success(list);                                                             //返回list
    }

    @ApiOperation(value = "获取办公室信息", response = OfficeVO.class)
    @GetMapping(value = "/office/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        Office office = officeMapper.selectById(id);                                            //查出办公室对象
        String collegeName = collegeService.getCollegeName(office.getCollegeId());              //获取学校名称
        String fieldName = fieldService.getFieldName(office.getFieldId());                      //获取场地名称
        String fieldTypeName = fieldTypeService.getFieldTypeName(office.getFieldTypeId());      //获取场地类型名称
        OfficeVO officeVO = new OfficeVO();                                                     //初始化VO
        BeanUtils.copyProperties(office, officeVO);                                              //对象复制赋值
        officeVO.setCollegeName(collegeName);                                                   //设置学校名称
        officeVO.setFieldName(fieldName);                                                       //设置场地名称
        officeVO.setFieldTypeName(fieldTypeName);                                               //设置场地类型名称
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", officeVO);                    //成功返回数据对象
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/office", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "fieldId", value = "场地id", paramType = "query", required = false),
            @ApiImplicitParam(name = "fieldTypeId", value = "场地类型id", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = true),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query", required = false),
            @ApiImplicitParam(name = "officeTableCount", value = "桌子数量", paramType = "query", required = false),
            @ApiImplicitParam(name = "compartmentFlag", value = "是否隔间", paramType = "query", required = false),
            @ApiImplicitParam(name = "maxVolumn", value = "最大容纳数量", paramType = "query", required = false),
            @ApiImplicitParam(name = "availableVolumn", value = "当前可容纳数", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger fieldId,
                         Integer fieldTypeId,
                         String name,
                         String englishName,
                         Integer officeTableCount,
                         Integer compartmentFlag,
                         Integer maxVolumn,
                         Integer availableVolumn,
                         BigInteger byWho) {
        Office office = new Office();                                          //初始化
        office.setCollegeId(collegeId);                                        //设置学校id
        office.setFieldId(fieldId);                                            //设置场地id
        office.setFieldTypeId(fieldTypeId);                                    //设置场地类型id
        office.setName(name);                                                  //设置办公室名称
        office.setEnglishName(englishName);                                    //设置办公室英文名称
        office.setOfficeTableCount(officeTableCount);                          //设置办公桌数
        office.setCompartmentFlag(compartmentFlag);                            //设置是否为隔间
        office.setMaxVolumn(maxVolumn);                                        //设置最大容纳人数
        office.setAvailableVolumn(availableVolumn);                            //设置当前可容纳人数
        office.setByWho(byWho);                                                //设置操作者
        officeMapper.insert(office);                                           //执行插入
        return ZQUResult.success();                                            //操作成功返回
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/office/{id}", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "fieldId", value = "场地id", paramType = "query", required = false),
            @ApiImplicitParam(name = "fieldTypeId", value = "场地类型id", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = true),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query", required = false),
            @ApiImplicitParam(name = "officeTableCount", value = "桌子数量", paramType = "query", required = false),
            @ApiImplicitParam(name = "compartmentFlag", value = "是否隔间", paramType = "query", required = false),
            @ApiImplicitParam(name = "maxVolumn", value = "最大容纳数量", paramType = "query", required = false),
            @ApiImplicitParam(name = "availableVolumn", value = "当前可容纳数", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger fieldId,
                            Integer fieldTypeId,
                            String name,
                            String englishName,
                            Integer officeTableCount,
                            Integer compartmentFlag,
                            Integer maxVolumn,
                            Integer availableVolumn,
                            BigInteger byWho) {
        Office office = officeMapper.selectById(id);                           //先查出旧记录
        office.setByWho(byWho);                                                //设置操作者
        officeMapper.updateById(office);                                       //更新操作者
        officeMapper.deleteById(id);                                           //逻辑删除
        office = new Office();                                                 //初始化新纪录
        office.setCollegeId(collegeId);                                        //设置学校id
        office.setFieldId(fieldId);                                            //设置场地id
        office.setFieldTypeId(fieldTypeId);                                    //设置场地类型id
        office.setName(name);                                                  //设置办公室名称
        office.setEnglishName(englishName);                                    //设置办公室英文名称
        office.setOfficeTableCount(officeTableCount);                          //设置办公桌数
        office.setCompartmentFlag(compartmentFlag);                            //设置是否为隔间
        office.setMaxVolumn(maxVolumn);                                        //设置最大容纳人数
        office.setAvailableVolumn(availableVolumn);                            //设置当前可容纳人数
        office.setByWho(byWho);                                                //设置操作者
        office.setLastVersion(id);                                             //设置上一版本
        officeMapper.insert(office);                                           //执行插入
        return ZQUResult.success();                                            //操作成功返回
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/office/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        Office office = officeMapper.selectById(id);                           //先查出旧记录
        office.setByWho(byWho);                                                //设置操作者
        officeMapper.updateById(office);                                       //更新操作者
        officeMapper.deleteById(id);                                           //逻辑删除
        return ZQUResult.success();                                            //操作成功返回
    }
}

